#ifndef _PLC_PARSER_H_
#define _PLC_PARSER_H_

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>

#include "types.h"
#include "opcodes.h"
#include "error.h"

#include "Program.h"

class Parser{
private:
	struct function;
	struct variable;

	struct function{
		char name[256];
		std::vector<variable> args;
		
		u64 offset;
	};

	struct variable{
		char name[256];
		u64 offset;

		u8 varSize;
		u16 arraySize;
	};

	struct parser_ret{
		u64 retOpcode;
		u64 retArg0;
		u64 retArg1;
	};

private:
	std::string line;
	std::vector<function> functions;
	std::vector<variable> variables;

	u64 codeOffset;
	u64 dataOffset;

	Program data;
	Program code;

private:
	std::string despaceLine(std::string line);
	u32 findFirstOf(std::string line, char ch);

	bool isDelimiter(char ch);
	u64 isReserved(std::string line);
	u64 isConstant(std::string line);
	u64 isVarName(std::string line);

	bool checkErrors(std::vector<parser_ret> *actionList);

	void loadTo(u8 dest, u64 org, u8 immediate);
	std::vector<parser_ret> parseLine(std::string line);

public:
	Parser(){
		data.setIncremental(4);
	}

	u64 parse();
};

#endif